package com.vvv.shortlink.admin.aop;


import com.vvv.shortlink.admin.annotation.AuthCheck;
import com.vvv.shortlink.common.dto.UserInfoDTO;
import com.vvv.shortlink.common.toolkit.UserContext;
import com.vvv.shortlink.common.convention.excepton.ClientException;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

import static com.vvv.shortlink.common.enums.UserErrorCodeEnum.NOT_LOGIN;

/**
 * @author vvv
 * @date 2024/2/1 14 37
 * discription
 */
@Aspect
@Component
public class LoginInterceptorAop {

    /**
     * 执行拦截
     *
     * @param joinPoint
     * @param authCheck
     * @return
     */
    @Around("@annotation(authCheck)")
    public Object doInterceptor(ProceedingJoinPoint joinPoint, AuthCheck authCheck) throws Throwable {
        String mustRole = authCheck.mustRole();
        // 当前登录用户
        UserInfoDTO userInfoDTO = UserContext.getUser();
        if (userInfoDTO == null) {
            throw new ClientException(NOT_LOGIN);
        }
        // 通过权限校验，放行
        return joinPoint.proceed();
    }
}
